package others.swing.p012;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

class Teacher extends Frame implements ActionListener {
    MenuBar menubar;//定义菜单条
    Menu menu1, menu2;//定义菜单
    MenuItem numItem, nameItem, classItem, sexItem, roomItem, clearItem, exitItem;//定义菜单项
    Panel panel1, panel2, panel3;//定义面板
    Label introduce, inquireLabel;//查找
    TextField inputTextField;//定义文本框
    Label showLabel, emptyLabel1, emptyLabel2;//定义标签
    TextArea text;//定义文本区
    Image myImage;//定义一个图像

    Teacher() {
        super("教师操作界面！");//设置窗体标题
        this.setFont(new Font("隶书", Font.BOLD, 20));//设置界面的字体
        this.setBackground(Color.pink);//设置界面背景颜色
        //初始化菜单
        menubar = new MenuBar();
        menu1 = new Menu("文件");
        menu2 = new Menu("查询");
        //初始化菜单项
        numItem = new MenuItem("按学号");
        nameItem = new MenuItem("按姓名");
        classItem = new MenuItem("按班级");
        sexItem = new MenuItem("按性别");
        roomItem = new MenuItem("按宿舍");
        clearItem = new MenuItem("清空");
        exitItem = new MenuItem("退出");
        myImage = new ImageIcon("宿舍3.jpg").getImage();//要显示的图片文件
        //初始化面板
        panel1 = new Panel();
        panel2 = new Panel();
        panel3 = new Panel();
        //初始化各个标签
        introduce = new Label("欢迎进入教师操作界面！");
        inquireLabel = new Label("请输入查询关键字：");
        showLabel = new Label("查询结果如下：");
        emptyLabel1 = new Label("              ");
        emptyLabel2 = new Label("              ");
        //初始化文本框
        inputTextField = new TextField(12);
        //初始化文本区，定义其不可编辑
        text = new TextArea(4, 40);
        text.setEditable(false);
        //将各个控件加入界面
        menu1.add(clearItem);
        menu1.add(exitItem);
        menu2.add(numItem);
        menu2.add(nameItem);
        menu2.add(classItem);
        menu2.add(sexItem);
        menu2.add(roomItem);
        menubar.add(menu1);
        menubar.add(menu2);
        setMenuBar(menubar);

        panel1.add(introduce);
        panel2.add(inquireLabel);
        panel2.add(inputTextField);
        panel3.add(showLabel);
        panel3.add(emptyLabel1);
        panel3.add(emptyLabel2);

        setLayout(new FlowLayout());//设置窗体上的各控件的布局为流式布局
        add(panel1);
        add(panel2);
        add(panel3);
        add(text);
        //注册各个控件的监听对象
        numItem.addActionListener(this);
        nameItem.addActionListener(this);
        classItem.addActionListener(this);
        sexItem.addActionListener(this);
        roomItem.addActionListener(this);
        clearItem.addActionListener(this);
        exitItem.addActionListener(this);

        //采用匿名类实现窗体标题栏上“×”按钮的关闭功能
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent ew) {
                dispose();   //关闭窗口并释放所占屏幕资源
            }
        });
        setBounds(100, 50, 500, 630);//设置窗体的起始坐标，以及宽度和高度
        setVisible(true);//让窗体可见
        setResizable(false);//让窗体不可修改

    }

    //插入一个图片
    public void paint(Graphics g) {
        g.drawImage(myImage, 25, 320, this);//图片的位置属性
    }

    @Override
    public void actionPerformed(ActionEvent ea) {
        //连接数据库
        Connection con;
        Statement sql;
        ResultSet rs;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException e) {
            System.out.println("" + e);
        }
        try {
            con = DriverManager.getConnection("jdbc:odbc:dormitory2", "", "");
            sql = con.createStatement();
            int flag = 0;
            String num, name, class1, sex, room, phone, input;
            input = inputTextField.getText();
            text.setForeground(Color.blue);
            //按学号查询
            if (ea.getSource() == numItem) {
                rs = sql.executeQuery("SELECT * FROM Room WHERE number='" + input + "'");
                while (rs.next()) {
                    num = rs.getString(1);
                    name = rs.getString(2);
                    class1 = rs.getString(3);
                    sex = rs.getString(4);
                    room = rs.getString(5);
                    phone = rs.getString(6);
                    text.append("学号：" + num + "|姓名：" + name + "|班级：" + class1 + "|性别：" + sex + "|宿舍：" + room + "|电话：" + phone + "\n");
                    flag = 1;
                }
            }

            //按姓名查询
            else if (ea.getSource() == nameItem) {
                rs = sql.executeQuery("SELECT * FROM Room WHERE name='" + input + "'");
                while (rs.next()) {
                    num = rs.getString(1);
                    name = rs.getString(2);
                    class1 = rs.getString(3);
                    sex = rs.getString(4);
                    room = rs.getString(5);
                    phone = rs.getString(6);
                    text.append("学号：" + num + "|姓名：" + name + "|班级：" + class1 + "|性别：" + sex + "|宿舍：" + room + "|电话：" + phone + "\n");
                    flag = 1;
                }
            }
            //按班级查询
            else if (ea.getSource() == classItem) {
                rs = sql.executeQuery("SELECT * FROM Room WHERE class='" + input + "'");
                while (rs.next()) {
                    num = rs.getString(1);
                    name = rs.getString(2);
                    class1 = rs.getString(3);
                    sex = rs.getString(4);
                    room = rs.getString(5);
                    phone = rs.getString(6);
                    text.append("学号：" + num + "|姓名：" + name + "|班级：" + class1 + "|性别：" + sex + "|宿舍：" + room + "|电话：" + phone + "\n");
                    flag = 1;
                }
            }
            //按性别查询
            else if (ea.getSource() == sexItem) {
                rs = sql.executeQuery("SELECT * FROM Room WHERE sex='" + input + "'");
                while (rs.next()) {
                    num = rs.getString(1);
                    name = rs.getString(2);
                    class1 = rs.getString(3);
                    sex = rs.getString(4);
                    room = rs.getString(5);
                    phone = rs.getString(6);
                    text.append("学号：" + num + "|姓名：" + name + "|班级：" + class1 + "|性别：" + sex + "|宿舍：" + room + "|电话：" + phone + "\n");
                    flag = 1;
                }
            }
            //按宿舍查询
            else if (ea.getSource() == roomItem) {
                rs = sql.executeQuery("SELECT * FROM Room WHERE room='" + input + "'");
                while (rs.next()) {
                    num = rs.getString(1);
                    name = rs.getString(2);
                    class1 = rs.getString(3);
                    sex = rs.getString(4);
                    room = rs.getString(5);
                    phone = rs.getString(6);
                    text.append("学号：" + num + "|姓名：" + name + "|班级：" + class1 + "|性别：" + sex + "|宿舍：" + room + "|电话：" + phone + "\n");
                    flag = 1;
                }
            }
            //清空文本区
            else if (ea.getSource() == clearItem) {
                inputTextField.setText(null);
                text.setText(null);
                flag = 1;
            }
            //退出
            else if (ea.getSource() == exitItem) {
                dispose();
                flag = 1;
            }
            con.close();
            if (flag == 0) {
                JOptionPane.showMessageDialog(this, "不存在相关信息！请重新输入！", "警告", JOptionPane.WARNING_MESSAGE);
            }
        } catch (SQLException e) {
            System.out.println(e);
        }
    }
}
